<!--
  * 学校信息
  *
  * @Author:    syf
  * @Date:      2025-05-14 10:36:40
  * @Copyright  畅想科技
-->
<template>
  <a-modal
      :title="form.placeId ? '编辑' : '添加'"

      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules"  >
          <a-row>
                    <a-form-item label="学校名称"  name="placeName">
                      <a-input v-model:value="form.placeName" placeholder="请输入学校名称" />
                    </a-form-item>
                    <!-- <a-form-item label="0:未删除；1:删除"  name="isDel">
                      <BooleanSelect v-model:value="form.isDel" style="width: 100%" />
                    </a-form-item> -->
          </a-row>

    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
    import { rSchoolApi } from '/@/api/support/school-api';
  import { smartSentry } from '/@/lib/smart-sentry';
          import BooleanSelect from '/@/components/framework/boolean-select/index.vue';

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
          placeId: undefined,
          placeName: undefined, //学校编号
  };

  let form = reactive({ ...formDefault });

  const rules = {
                  placeName: [{ required: true, message: '学校名称 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.schoolId) {
        await rSchoolApi.update(form);
      } else {
        await rSchoolApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
